﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Mani.Patient.Business.Services;
using System.Threading;
using System.Globalization;

public partial class _Default : System.Web.UI.Page {

    public IPatientService PatientService { get; set; }

    protected override void InitializeCulture() {
        MyCulture.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e) {

        if (!Page.IsPostBack) {
            FindPatients(0, true);
        }
    }

    private void FindPatients(int pageIndex, bool setPageCount) {

        string fileNumber = txtFileNumber.Text;
        string patientName = txtPatientName.Text;

        if (setPageCount)
            gvPatients.VirtualItemCount = PatientService.CountFindPatients(patientName, fileNumber);

        gvPatients.DataSource = PatientService.FindPatients(15, pageIndex, "Id", false, patientName, fileNumber);
        gvPatients.DataBind();
        
    }

    protected void btnSearch_Click(object sender, EventArgs e) {
        FindPatients(0, true);
    }

    protected void gvPatients_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        gvPatients.PageIndex = e.NewPageIndex;
        FindPatients(e.NewPageIndex, false);
    }



    protected void gvPatients_RowDeleting(object sender, GridViewDeleteEventArgs e) {
        int id = Convert.ToInt32(gvPatients.DataKeys[e.RowIndex].Value);
        PatientService.DeletePatient(id);
        FindPatients(0, true);
    }

    protected void gvPatients_Sorting(object sender, GridViewSortEventArgs e) {

    }


    protected void gvPatients_RowDataBound(object sender, GridViewRowEventArgs e) {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        Control deleteButton = e.Row.FindControl("lbDeletePatient");

        if (deleteButton != null) {
            string message = GetLocalResourceObject("DeleteMessage").ToString();    
            ((LinkButton)deleteButton).OnClientClick = String.Format("javascript:return confirm('{0}');", message);
        }
    }
}
